<?php
/**
 * @name    一键采集模块
 * @package GXCMS.Administrator
 * @link    www.gxcms.com
 */
class Lol178Model extends Model {	
	
    private $lol178;
    private $doc; //DOMDocument对象
    private $xpath;//DOMXpath对象
    private $xpath_query; //html Xpath路径
    private $url; //要解析的文档地址
    private $body; //解析出来的html body节点
    private $page_xpath; //页码的xpath地址
    private $real_url_xpath;
    function __construct(){        
		$this->lol178=D('video'); //实例化数据库对象       
    }
    
    public function init($lol_config){
        if(empty($lol_config) || !is_array($lol_config))
            die('lol config error,please check it');//检查传递过来的数据
        $this->xpath_query = $lol_config['xpath'];
        $this->url = $lol_config['url']; 
        $this->page_xpath = $lol_config['page_xpath'];
        $this->real_url_xpath = $lol_config['real_url_xpath'];
        $this->doc = new DOMDocument();  //实例化Dom对象
        $this->doc->loadHTMLFile($this->url) or die('lol.178 url is error!');
        $this->body = $this->doc->getElementsByTagName('body')->item(0);
        $this->xpath = new DOMXPath($this->doc);//实例化Xpath对象
    }
    public function get_list_data($i = 1) {
        $list = $this->xpath->evaluate($this->xpath_query, $this->body);
       // echo $this->xpath_query;
       // var_dump($list);die;
        $i = 0;
        foreach ($list as $key => $val) {
            $a = $val->getElementsByTagName('a')->item(0);
            $data['title'] = $a->getAttribute('title');
            $data['url'] = $a->getAttribute('href');
            $img = $val->getElementsByTagName('img')->item(0);			
            $data['picurl'] = $img->getAttribute('src');
			$data['vid'] = $img->getAttribute('name');
			//$data['picurl'] = $this->down_img($data['img']);
			$data['cid'] = 40 ;
            //$time = $val->getElementsByTagName('span')->item(1);
            //$data[$key]['time'] = $time->textContent;
			$publish = $val->getElementsByTagName('span')->item(3);
            $data['addtime'] = strtotime($publish->textContent);
			if(empty($data['addtime'])){
				$data['addtime'] = time();
			}
            // $this->lol178->data($data)->add();unset($data);
			$i++;
			// file_put_contents('v.txt',var_export($data,true));
            // break;
        }
		echo $i;

		
//            $data[]['time'] = str_replace('发布：', '',$data[]['time']);
        
        // var_dump($data);
    }
	
	public function down_img($url ="http://g3.ykimg.com/0100641F46515D3A9E5D1C048FA1BF46224811-28E8-CD2A-7C53-CC9A5AA0D595"){
		$imgPath   = './'.C('upload_path').'/'.'video/'.date(C('upload_style'),time()).'/' . time() . ".jpg";
		$get_file  = get_collect_file($url);
		if ($get_file) {
		   write_file($imgPath,$get_file);
		   return $imgPath;
		}
		else{
			return $url;
		}
		 
	}
//    public function get_list_data($type=""){
//        $list = $this->xpath->evaluate($this->xpath_query,$this->body);
////        echo $this->xpath_query;
////        var_dump($list);die;
//        $data = array();
//        foreach ($list as $key => $val){
//            $a = $val->getElementsByTagName('a')->item(0);
//            $data[$key]['title'] =  $a->getAttribute('title');
//            $data[$key]['url'] = $a->getAttribute('href');
//            $data[$key]['real_url'] = $this->get_real_url($data[$key]['url']);
//            $p = $val->getElementsByTagName('p')->item(0);
//            $data[$key]['img'] = $p->getAttribute('style');
//            $b = $val->getElementsByTagName('b')->item(0);
//            $data[$key]['time'] = str_replace('发布：', '',$b->textContent);
//            if(!empty($type) && $type == "today"){
//                $time = strtotime($data[$key]['time']);//计算获取时间的时间戳
//                $nowtime = mktime(0,0,0,date('m'),date('d'),date('Y'));//当天的0：00：00的时间戳
//                if($time < $nowtime){ //如果获取的时间小于当前的时间，也就是说今天没有新的视频，不予采集   
//                    unset($data[$key]);
//                    break;
//                }             
//            }
//            
//            
////            $data[]['time'] = str_replace('发布：', '',$data[]['time']);
//        }
//        var_dump($data);
//    }
    
    
    //如果是视频页面是两场视频的话，获取地址失败！待修复
    public function get_real_url($url){
        if (empty($url)) die("function:".__FUNCTION__."行：".__LINE__."throw error" );
        $html = new DOMDocument();
        $html->loadHTMLFile($url) ;
        $list = $html->getElementsByTagName('embed');        
        $real_url = array();     
        foreach($list as $key =>$con){
            $real_url[$key] = $list->item($key)->getAttribute('src');
        }
        return $real_url;
        
        
    }
    
    function get_page_num(){
         $page = $this->xpath->evaluate($this->page_xpath,$this->body);
		 return $page->length;
         
    }
    
	
	
}
?>